package com.just.dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import java.util.Iterator;

public class UseIteratorTest {
    public static void main(String[] args){
        Document document=ParseXML.parse("D:\\ideaProjects\\dom4jdemo\\src\\main\\resources\\xml\\demo1.xml");
        bar(document);
    }
    public static void bar(Document document){
        Element root=document.getRootElement();
        //利用element的elementIterator方法
        for(Iterator it = root.elementIterator(); it.hasNext();){
           Element element=(Element) it.next();
           //do something
           System.out.println(element.getName()+":");
           for(Iterator last = element.elementIterator(); last.hasNext();){
               Element lastElement=(Element)last.next();
               System.out.println("  "+lastElement.getName()+":"+lastElement.getText());
           }
        }
        System.out.println("-----☜-----华丽丽的分割线-----☞-----");
        //指定遍历哪个元素
        for(Iterator it = root.elementIterator("man"); it.hasNext();){
            Element element=(Element) it.next();
            //do something
            System.out.println(element.getName()+":");
            for(Iterator last = element.elementIterator(); last.hasNext();){
                Element lastElement=(Element)last.next();
                System.out.println("  "+lastElement.getName()+":"+lastElement.getText());
            }
        }
        System.out.println("-----☜-----华丽丽的分割线-----☞-----");
        //遍历属性
        for(Iterator it=root.attributeIterator();it.hasNext();){
            Attribute attribute=(Attribute) it.next();
            System.out.println(attribute.getName()+":"+attribute.getValue());
        }


    }
}
